<%--
  Created by IntelliJ IDEA.
  User: Andrew
  Date: 19.02.14
  Time: 16:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
        errorPage="error.jsp" %>
<html>
<head>
    <title>Simple Calculator</title>
</head>
<body>
    <%
        String firstParam = request.getParameter("a");
        String secondParam = request.getParameter("b");
    %>
    <form method="get"/>
        <input type="text" name="a" value="<%= firstParam == null ? "" : firstParam%>">
        <input type="text" name="b" value="<%= secondParam == null ? "" : secondParam%>">
        <input type="submit" name="op" value="+">
        <input type="submit" name="op" value="-">
        <input type="submit" name="op" value="*">
        <input type="submit" name="op" value="/">
    </form>
    <%
        String res;
        String operation = request.getParameter("op");
        if(firstParam != null && secondParam != null && operation != null) {
            int a = Integer.parseInt(firstParam);
            int b = Integer.parseInt(secondParam);
            int c;
            if("+".equals(operation)) {
                c = a + b;
            } else if("-".equals(operation)) {
                c = a - b;
            } else if("*".equals(operation)) {
                c = a * b;
            } else if("/".equals(operation)) {
                c = a / b;
            } else {
                throw new RuntimeException();
            }
            res = firstParam + " " + operation + " " + secondParam + " = " + Integer.toString(c);
        } else {
            res = "";
        }

    %>
    <%=res%>
</body>
</html>
